…items` by 670% in PR #574 (`easier-benchmark`)
Here’s how you can optimize your code for **speed** and **memory** while maintaining return values, behavior, and comments. I focused on making `pytest_collection_modifyitems` faster by.
- **Avoiding function calls/attribute checks inside tight loops:**
Since ideally `get_closest_marker` existence is consistent across items, fetch it once per item and cache the lookup.
- **Reducing repeated work:**
Move more work (e.g. creation of the skip marker) outside the inner loop.
### Changes and Optimizations.
- **Cache Attribute**:
`getattr(item, "get_closest_marker", None)` is used to avoid repeated `hasattr` checks or repeated attribute lookups.
- **Reuse Marker Instance**:
`skip_marker` is created once, not in every loop iteration.
- **Skip on missing attribute** instead of raising.
**This will speed up the loop by:**
- Reducing per-item attribute lookup
- Reducing decorator construction
- Reducing function calls on items which don’t have `get_closest_marker` (if any)
---
Let me know if you want further or different optimizations!
PR Type
Enhancement
Description
Add debugging prints in trace wrapper
Introduce frozen
BenchmarkKeydataclassExtend module path traversal logic
Refine pytest marker handling and skip logic
Diagram Walkthrough
File Walkthrough
codeflash_trace.py
Add debug prints in trace wrappercodeflash/benchmarking/codeflash_trace.py
plugin.py
Introduce BenchmarkKey and marker handlingcodeflash/benchmarking/plugin/plugin.py
BenchmarkKeydataclass with__str__.traverse_up=Trueto module name resolution.code_utils.py
Enhance module path traversal logiccodeflash/code_utils/code_utils.py
traverse_upparameter to fallback relative path logic.pytest_new_process_discovery.py
Register benchmark marker in discovery plugincodeflash/discovery/pytest_new_process_discovery.py
pytest_configureto registerbenchmarkmarker.optimizer.py
Refactor run_benchmarks flowcodeflash/optimization/optimizer.py
console.rule()before benchmarks.